Start a Mosquitto container first. For example:
codes\_demo\1_start_broker.sh
to start a Mosquitto container on Raspberry Pi.mqtt_config\mqtt
.allow_anonymous true
in mqtt_config\mqtt\config\mosquitto.conf
to allow anonymous client.
In [1]:
import os
import sys
import time
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, os.path.sep.join(['..', 'codes']), 'client')))
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, os.path.sep.join(['..', 'codes']), 'node')))
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, os.path.sep.join(['..', 'codes']), 'shared')))
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, os.path.sep.join(['..', 'codes']), 'micropython')))
import client
from collections import OrderedDict
In [2]:
the_client = client.Client()
the_client.start()
while not the_client.status['Is connected']:
time.sleep(1)
print('Node not ready yet.')
In [3]:
# messages _____________________________________________
messages = OrderedDict()
messages['read_GPIOs'] = {'message_type': 'command',
'command': 'read GPIOs',
'kwargs': {'pins': [5, 12, 13, 14, 15, 16]},
'need_result': True}
messages['blink_led'] = {'message_type': 'command',
'command': 'blink led',
'kwargs': {'times': 3, 'forever': False,
'on_seconds': 0.1, 'off_seconds': 0.1}}
In [4]:
# remote_nodes = ['n_Lambda', 'n_Alpha', 'n_Beta']
In [5]:
# remote_node = 'n_Alpha'
# messages_ext = {}
# messages_ext['blink_led'] = {'message_type': 'command',
# 'command': 'blink led',
# 'kwargs': {'times': 3, 'forever': False, 'on_seconds': 0.1, 'off_seconds': 0.1}}
# the_client.request('Hub', messages_ext['blink_led'])
# messages_ext['write_GPIOs'] = {'message_type': 'command',
# 'command': 'write GPIOs',
# 'kwargs': {'pins_and_values': [(2, 0), (2, 1), (2, 0),]}}
# the_client.request(remote_node, messages_ext['write_GPIOs'])
# messages_ext['test_eval'] = {'message_type': 'eval',
# 'to_evaluate': '2+3',
# 'need_result': True}
# _, result = the_client.request(remote_node, messages_ext['test_eval'])
# print('result:', result.get())
# messages_ext['test_exec'] = {'message_type': 'exec',
# 'to_exec': 'print("Hello World!")'}
# the_client.request(remote_node, messages_ext['test_exec'])
# with open('script_to_deploy.py') as f:
# script = f.read()
# messages_ext['test_upload_script'] = {'message_type': 'script',
# 'script': script}
# the_client.request(remote_node, messages_ext['test_upload_script'])
# messages_ext['test_function'] = {'message_type': 'function',
# 'function': 'blink_led',
# 'kwargs': {'times': 3, 'forever': False,
# 'on_seconds': 0.1, 'off_seconds': 0.1}}
# the_client.request(remote_node, messages_ext['test_function'])
In [6]:
the_client.node.worker.roll_call()
time.sleep(2)
remote_nodes = sorted(the_client.node.worker.contacts.keys())
print('\n[____________ Connected nodes ____________]\n')
print('\nConnected nodes:\n{}\n'.format(remote_nodes))
In [7]:
# remote_nodes = ['n_Lambda', 'n_Alpha', 'n_Beta']
In [8]:
for remote_node in remote_nodes:
the_client.request(remote_node, messages['blink_led'])
In [5]:
the_client.request('Hub', messages['blink_led'])
Out[5]:
In [12]:
for remote_node in remote_nodes:
_, result = the_client.request(remote_node, messages['read_GPIOs'])
print('\nGPIO status for {}: {}\n'.format(remote_node, result.get()))
In [10]:
print('\n[______________ Sending messages ______________]\n')
results = []
# send out the messages
for message in messages.values():
for remote_node in remote_nodes:
if remote_node != the_client.node.worker.name:
time.sleep(0.1) # PyCharm needs this delay.
formatted_message, asynch_result = the_client.request(remote_node, message)
results.append((formatted_message, asynch_result))
In [11]:
# collect and print results
print('\n[_________ Wait few seconds for reply _________]\n')
for (message, result) in results:
try:
if message.get('need_result'):
print('\n[Result for request]:\n___Request___:\n{0}\n___Result____:\n{1}\n'.format(message,
result.get() if result else None))
except Exception as e:
print('\n[{}]\nMessage:\n{}'.format(e, message))
# Wait a while
time.sleep(3)
In [11]:
# Stopping
the_client.stop()
the_client = None
print('\n[________________ Demo stopped ________________]\n')
In [ ]: